package com.sunday.player.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

public class fileManager {
    private final String TAG = "fileManager";
    public static File SDCARDPATH = Environment.getExternalStorageDirectory();
    public fileManager() {
    
    }
    /**
     * SDCARD가 활성화 상태인가 ?
     * @return
     */
    public boolean isSDCARD_Mounted(){
	if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
	    return true;
	}
	return false;
    }
    /**
     * 선택한 하위폴더를 탐색하여 하위
     * 폴더의 리스트를 돌려준다
     * @param targetFolder
     * @return
     */
    public String[] getFolder(String targetFolder){
	File folder = new File(targetFolder);
	if(folder == null){
	    return new String[]{};
	}
	
	return folder.list();
    }
    
    /**
     * 선택한 폴더의 mp3 파일 개수 
     * @param targetFolder
     * @return
     */
    public int getFolderFileCount(String targetFolder){
	
	File folder = new File(targetFolder);
	File[] array = folder.listFiles(new mp3FileFilter());
	if(array == null){
	    return 0;
	}
	
	return folder.listFiles(new mp3FileFilter()).length;
    }
    /**
     * 재생가능한 파일의 합
     * @param tatgetFolder
     * @param isSub 하위폴더도 검색할려능가 ?
     * @return
     */
     public int getFolderAllCount(String tatgetFolder , boolean isSub){
	 
	 int total = 0;
	 String[] target  = getFolder(tatgetFolder);
	 if(target == null){
	     return 0;
	 }
	for(String folder : target){
                int count = getFolderFileCount(tatgetFolder+"/"+folder);
                if(count != 0){
                total += count;
                playerControl.Trace_i("  --  "+tatgetFolder+"/"+ folder +" = "+ count+"  =    total", TAG);
                }
                if(isSub){
                    total += getFolderAllCount(tatgetFolder+"/"+folder, isSub);
                }
	}
	 
	 return total;
     }
     
     /**
      *  선택한 폴더의 하위폴더를 포함한 MP3  파일 리스트
      * @param tatgetFolder
      * @param isSub
      * @return
      */
     public List<String> getFolderAllList(String tatgetFolder, boolean isSub){
	 
	 List<String> filelist = new ArrayList<String>();
	 String[] target  = getFolder(tatgetFolder);
	 if(target == null){
	     return filelist;
	 }
	 
	 for(String folder : target){
	     File[] sublist = getFolderFileList(tatgetFolder+"/"+folder);
	     if(sublist != null){
		 
		 for(File filename : sublist){
		     filelist.add(filename.getPath());
		     playerControl.Trace_i("filename" + filename, TAG);
		 }
	     }
	     if(isSub){
		 filelist.addAll(getFolderAllList(tatgetFolder+"/"+folder, isSub) );
	     }
	     
	 }
	 
	 
	 return filelist;
     }
     
     /**
      * 서브 폴더 목록
      * @param folder
      * @return
      */
     public String[] getSubFolderList(String folder){
	 return new File(folder).list(); 
     }
     /**
      * 폴더의 재생가능한 파일이름 목록
      * @param folder
      * @return
      */
     public File[] getFolderFileList(String folder){
	 return new File(folder).listFiles(new mp3FileFilter()); 
     }
    
    
}
